﻿using System.Web.Mvc;
using System.Web.Routing;
using BusinessBingo.Web.Filters;
using BusinessBingo.Web.Infrastructure;
using Castle.Core;
using Combres;
using MvcMiniProfiler;

namespace BusinessBingo.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);

            RegisterCombresRoute(RouteTable.Routes);
            RegisterRoutes(RouteTable.Routes);

            InitApp(LifestyleType.PerWebRequest);
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new Localization());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Group", // Route name
                "Group/Play/{groupUrl}/{gameNumber}", // URL with parameters
                new
                {
                    controller = "Group",
                    action = "Play",
                }
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new
                    {
                        controller = "Home",
                        action = "Index",
                        id = UrlParameter.Optional
                    }
                );
        }

        private static void RegisterCombresRoute(RouteCollection routes)
        {
            routes.AddCombresRoute("Combres");   
        }

        public void InitApp(LifestyleType lifestyleType)
        {
            Bootstrapper.InstallDependencies(lifestyleType);
            DependencyResolver.SetResolver(new WindsorDependencyResolver(Bootstrapper.Container));
        }

        protected void Application_BeginRequest()
        {
            if (Request.IsLocal)
            {
                MiniProfiler.Start();
            }
        }

        protected void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }
    }
}